﻿


using System;
using Microsoft.Win32;

class SelectRegisterByAppName
{
    // 右键菜单相关的子路径
    private static readonly string[] shellPaths = new string[]
    {
        @"Directory\shell",
        @"Directory\Background\shell",
        @"*\shell",
        @"Drive\shell",
        @"Drive\Background\shell"
    };

    /// <summary>
    /// 搜索注册表中包含 appName 的右键菜单键
    /// </summary>
    public static void StartSearch(string appName, RegistryKey rootKey)
    {
        foreach (var shellPath in shellPaths)
        {
            using (RegistryKey baseKey = rootKey.OpenSubKey(shellPath))
            {
                if (baseKey == null) continue;

                foreach (var subKeyName in baseKey.GetSubKeyNames())
                {
                    if (subKeyName.IndexOf(appName, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        Console.WriteLine($"{rootKey.Name}\\{shellPath}\\{subKeyName}");
                    }
                }
            }
        }
    }
}

class SelectAll
{
    public static void Run(string appName)
    {

        Console.WriteLine("---------------------------------------------------------------------------");
        Console.WriteLine($"搜索 HKCU : {appName}");
        SelectRegisterByAppName.StartSearch(appName, Registry.CurrentUser);

        Console.WriteLine($"搜索 HKCR : {appName}");
        SelectRegisterByAppName.StartSearch(appName, Registry.ClassesRoot);

        Console.WriteLine("搜索完成。");
    }
}





// 输出结果太杂乱:
// 搜索 QQ 只需要返回  
    // HKEY_CURRENT_USER\Directory\shell\QQ 或者 HKEY_CLASS_ROOT\Directory\backgroud\shell\QQ
    // 这么简单的结果就行了
    
